<?php
// +----------------------------------------------------------------------
// | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2017 http://www.thinkcmf.com All rights reserved.
// +----------------------------------------------------------------------
// | Author: pl125 <xskjs888@163.com>
// +----------------------------------------------------------------------

namespace api\portal\model;


use think\Model;

class MerchModel extends Model
{
    /**
     * 模型名称
     * @var string
     */
    protected $name = 'merch';

    //模型关联方法
    protected $relationFilter = ['merch'];


    /**
     * more 自动转化
     * @param $value
     * @return string
     */
    public function getAvatarAttr($value)
    {
        $value = !empty($value) ? cmf_get_image_url($value) : $value;
        return $value;
    }

    /**
     * 关联 user表
     * @return string 关联数据
     */
    public function user()
    {
        return $this->belongsTo('UserModel', 'user_id')->setEagerlyType(1);
    }

    /**
     * 获取二维码
     * @param $accessToken
     * @param $page
     * @param $scene
     */
    public static function getQrcode($accessToken, $page, $scene)
    {

        $sendData = [
//            'page' => 'goods/pages/goods/details',
            'scene' => $scene,
        ];

        $res = cmf_curl_post('https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token=' . $accessToken, json_encode($sendData));

        $patch = 'static/images/qrcode/' . md5($page . $scene) . '.png';
        file_put_contents(public_path() .$patch, $res);

        return cmf_get_domain() .'/'. $patch;

    }
}
